package cn.wolfcode.rbac.util;

import cn.wolfcode.rbac.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

//用户对象的上下文工具类
public abstract class UserContext {
    public static final String EMP_IN_SESSION = "EMP_IN_SESSION";
    public static final String EXP_IN_SESSION = "EXP_IN_SESSION";

    private static HttpSession getSession() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    //把当前登录成功的用户对象存入session
    public static void setCurrentUser(Employee emp) {
        getSession().setAttribute(EMP_IN_SESSION, emp);
    }

    public static Employee getCurrentUser() {
        return (Employee) getSession().getAttribute(EMP_IN_SESSION);
    }

    //把当前登录成功的用户权限表达式存入session
    public static void setExpressions(List<String> exps) {
        getSession().setAttribute(EXP_IN_SESSION, exps);
    }

    public static List<String> getExpressions() {
        return (List<String>) getSession().getAttribute(EXP_IN_SESSION);
    }
}
